<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="Banner位置">
          <el-input v-model="form.position" placeholder="Banner位置" />
        </el-form-item>
        <el-form-item label="上架状态">
          <el-select v-model="form.status" placeholder="状态">
            <el-option label="已上架" value="1" />
            <el-option label="未上架" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onAdd">新增</el-button>
          <el-button type="primary" plain @click="onExport">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="title" label="标题" align="center" width="150" />
          <el-table-column prop="description" label="说明" align="center" width="150" />
          <el-table-column
            prop="position"
            label="位置"
            align="center"
          />
          <el-table-column
            prop="lang"
            label="语言"
            align="center"
          />
          <el-table-column label="图片" align="center" width="100">
            <template #default="scope">
              <el-image
                style="width: 70px; height: 70px"
                :src="scope.row.image_show"
                alt=""
                fit="fill"
                :preview-src-list="[scope.row.image_show]"
                :preview-teleported="true"
              ></el-image>
            </template>
          </el-table-column>

          <!-- <el-table-column prop="url" label="跳转链接" align="center"  width="180"/> -->
          <el-table-column
            prop="go_type"
            label="跳转类型"
            align="center"
            width="100"
          />
          <el-table-column
            prop="view_name"
            label="View名称"
            align="center"
            width="100"
          />
          <el-table-column
            prop="params"
            label="跳转参数"
            align="center"
            width="220"
          />
          <el-table-column
            prop="is_login"
            label="跳转否需要登录"
            align="center"
            :formatter="formatter"
          />
          <el-table-column
            prop="priority"
            label="排序"
            align="center"
          />
          <el-table-column
            prop="status"
            label="上架状态"
            align="center"
            :formatter="formatter"
          />
          <el-table-column
            prop="createtime"
            label="添加时间"
            align="center"
            :formatter="formatterTime"
            width="180"
          />
          <el-table-column
            prop="updatetime"
            label="更新时间"
            align="center"
            :formatter="formatterTime"
            width="180"
          />
          <el-table-column
            label="操作"
            align="center"
            width="120"
            fixed="right"
          >
            <template #default="scope">
              <el-button
                type="primary"
                plain
                @click="handleOperate(scope.$index, scope.row, 'edit')"
                >编辑</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      @handleUploaderImgRequest="handleUploader"
      :dialogFormVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { formatterTime, exportToExcel, formatter } from "@/utils/utils";
import { bannerListApi, bannerOperateApi } from "@/api/moby/activity/activity";
import DialogForm from "./components/dialogForm.vue";
const tableData = ref([]);
const loading = ref(false);
const dialogVisible = ref(false);
const btnLoading = ref(false);
const formData = {
  position: "",
  status: "",
};
const form = reactive({ ...formData });
const dialogFormData = {
  banner_id: "",
  name: "",
  position: "home",
  lang: "en",
  image: "",
  // url: "",
  params: "",
  priority: "",
  status: "",
  is_login: "",
  imageUrl: "",
  description: "",
  go_type: "",
};
const dialogForm = reactive({ ...dialogFormData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    position: form.position,
    status: form.status,
  };
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await bannerListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
  getTbaleData();
};
//新增
const onAdd = () => {
  dialogVisible.value = true;
};
const handleOperate = async (index, row, type) => {
  // 编辑
  if (type === "edit") {
    Object.assign(dialogForm, {
      ...row,
      banner_id: row.id,
      name: row.view_name,
    });
    dialogVisible.value = true;
  }
};
//关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false;

  Object.assign(dialogForm, dialogFormData);
};

// 取消
const cancel = () => {
  dialogVisible.value = false;

  Object.assign(dialogForm, dialogFormData);
};
// 提交
const submit = async (param) => {
  param.name = param.view_name;
  delete param.id;
  delete param.createtime;
  delete param.updatetime;
  try {
    btnLoading.value = true;
    const res = await bannerOperateApi(param);

    ElMessage({
      message: res.message,
      type: "success",
    });

    dialogVisible.value = false;
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    btnLoading.value = false;
    dialogVisible.value = false;
  }
};
// //上传图标
const handleUploader = (res, name, src) => {
  if (res.code === 0) {
    ElMessage({
      message: "图片上传成功",
      type: "success",
    });
    // dialogForm.image = res.data.name || "";
    // dialogForm.imageUrl = res.data.pic_url || "";
    dialogForm[`${src}`] = res.data.pic_url || "";
    dialogForm[`${name}`] = res.data.name || "";
  }
};
// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1 });
  const res = await bannerListApi(data);

  let head = {
    id: "ID",
    name: "名称",
    position: "位置",
    lang: "语言",
    image: "banner图片",
    url: "跳转链接",
    params: "跳转参数",
    priority: "排序",
    status: "上架状态",
    is_login: "banner是否需要登录",
    createtime: "添加时间",
    updatetime: "更新时间",
  };
  if (res.data.results.length) {
    exportToExcel(res.data.results, head, "Mobybox-Banner管理");
  } else {
    ElMessage({
      message: "暂无数据可导出",
      type: "error",
    });
    return;
  }
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
